package pro.spring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pro.spring.inter.UsupportMapper;
import pro.spring.model.Usupport;
import pro.spring.vo.PaginationVo;

@Service
public class UsupportService{

	@Autowired
	private UsupportMapper UsupportMapper;
	
	public int deleteByPrimaryKey(Integer id) {
		return UsupportMapper.deleteByPrimaryKey(id);
	}

	public int insert(Usupport record) {
		return UsupportMapper.insert(record);
	}

	public Usupport selectByPrimaryKey(Integer id) {
		return UsupportMapper.selectByPrimaryKey(id);
	}
	
	public int selectCountBydsId(Integer id) {
		return UsupportMapper.selectCountBydsId(id);
	}

	public int updateByPrimaryKey(Usupport record) {
		return UsupportMapper.updateByPrimaryKey(record);
	}
	
	public Usupport selectZan(Integer uId,Integer udis) {
		return UsupportMapper.selectZan(uId, udis);
	}
	
	public PaginationVo<Usupport> getPaginationInfo(int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Usupport> pageVo=new PaginationVo<Usupport>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UsupportMapper.selectCount());
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		pageVo.setList(UsupportMapper.selectListPage(pageVo.getOffset(), pageVo.getRows()));
		return pageVo;
	}
	
	
	public Integer dianzhan(Integer uId,Integer dsId ){
		Integer num =0;
		if(UsupportMapper.selectZan(uId, dsId)==null){//关注
			Usupport u = new Usupport();
			u.setUdiscussDsId(dsId);
			u.setUsersUId(uId);
			if(UsupportMapper.insert(u)==1){
				num = 2;
			}
		}else{//取消关注
			num = UsupportMapper.deleteByPrimaryKey(UsupportMapper.selectZan(uId, dsId).getSuId());
		}
		return num;
	}
	
}
